capture log close
clear all
set maxvar 10000
set more off
pause off
# delimit;

local data "" ;
local output "" ;

foreach sample in all { ;

	use "prwora_bw264_crime_poly1allcov_iv_`sample'.dta", clear ;
	
	keep if 
		(
		 variables == "n_1crime2genr_1997_2017"  | variables == "n_2crime2genr_1997_2017"  | variables == "n_3crime2genr_1997_2017"  | variables == "n_4crime2genr_1997_2017" |
		 variables == "n_1crime2ahinc_1997_2017" | variables == "n_2crime2ahinc_1997_2017" | variables == "n_3crime2ahinc_1997_2017" | variables == "n_4crime2ahinc_1997_2017" |
		 variables == "avg_1onssi_1997_2017" 	 | variables == "avg_2onssi_1997_2017" 	   | variables == "avg_3onssi_1997_2017" 	 | variables == "avg_4onssi_1997_2017" 	 |
		)	
		& sample == "age18unfav" ;
	forval i = 1(1)4 { ;
		replace variables = "genre`i'"   if variables == "n_`i'crime2genr_1997_2017" ;
		replace variables = "ahinc`i'"   if variables == "n_`i'crime2ahinc_1997_2017" ;
		replace variables = "onssi`i'"   if variables == "avg_`i'onssi_1997_2017" ;
	} ;
	forval i = 1(1)3 { ;
		replace variables = "khinc`i'"   if variables == "avg_`i'kidinchh_1997_2017" ;
	} ;
	gen var = substr(variables,1,5) ;
	gen period = substr(variables,-1,1) ;
	destring period, replace ;

	rename n_age18unfav b_endo ;
	rename sen_age18unfav se_endo ;
	keep var period b_endo se_endo mean_rd ;

	gen lo95 = b_endo - 1.96*se_endo ;
	gen hi95 = b_endo + 1.96*se_endo ;


	gen 	sort = 2.5 	if  var == "onssi" & period == 1 ;
	replace	sort = 3 	if  var == "genre" & period == 1 ;
	replace	sort = 3.5 	if  var == "ahinc" & period == 1 ;

	replace	sort = 7.5 	if  var == "onssi" & period == 2 ;
	replace	sort = 8 	if  var == "genre" & period == 2 ;
	replace	sort = 8.5 	if  var == "ahinc" & period == 2 ;

	replace	sort = 12.5 	if var == "onssi" & period == 3 ;
	replace	sort = 13 		if var == "genre" & period == 3 ;
	replace	sort = 13.5 	if var == "ahinc" & period == 3 ;

	replace	sort = 17.5 	if var == "onssi" & period == 4 ;
	replace	sort = 18 		if var == "genre" & period == 4 ;
	replace	sort = 18.5 	if var == "ahinc" & period == 4 ;

		
	twoway 
	
		(rcap hi95 lo95 sort if var == "genre", color(orange*.5) lwidth(medthick) yaxis(1))
		(rcap hi95 lo95 sort if var == "ahinc", color(red*.75) lwidth(medthick) yaxis(1))
		(rcap hi95 lo95 sort if var == "onssi", color(blue*.5) lwidth(medthick) yaxis(2))
		
		(scatter b_endo sort if var == "genre", color(orange) msize(vlarge) msymbol(X) yaxis(1))
		(scatter b_endo sort if var == "ahinc", color(red*1.5)  msize(large) msymbol(S) yaxis(1))
		(scatter b_endo sort if var == "onssi", color(blue)   msize(large) msymbol(O) yaxis(2))
		
		(pcarrowi -.09 8.5 	-.08 8  	"On SSI")
		(pcarrowi .02 11 	.05 12.5  	(6) "Inc-gen charge")
		(pcarrowi .01 14.5 	.02 14  	"Incarceration")
		, 
		legend(off)
		xsc(r(0 20)) xlabel(2.5 "1997-01" 7.5 "2002-06" 12.5 "2007-12" 17.5 "2013-17") xtitle("")
					 ylab(-.12(.06).12, axis(1)) ytitle("Crime IV estimate", size(medsmall) axis(1))
					 ylab(-.5(.25).5, axis(2)) ytitle("On SSI IV estimate", size(medsmall) axis(2))
		graphregion(color(white))
		bgcolor(white)
		ylabel(,nogrid angle(0))
		yline(0,lcolor(gray))
		;
		graph export "`output'/fig5.png", width(1000) height(750) replace ;
	
} ;



